<?php

namespace App\Http\Requests;

use App\Models\Order;

class ReviewOrderRequest extends BaseRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $orderItems = $this->user()->orderItems()->with('order')->get();
        return [
            "order_items.*.id" => [
                'required',
                function ($attribute, $order_items_id, $fail) use ($orderItems) {
                    if (!$orderItem = $orderItems->where('id', $order_items_id)->first()) {
                        return $fail('id不存在');
                    }
                    if (!$orderItem->order->paid_at) {
                        return $fail('你的订单还有没有支付');
                    }
                    if ($orderItem->order->refund_status != Order::REFUND_STATUS_PENDING) {
                        return $fail('你的订单在退款流程中');
                    }
                    if ($orderItem->order->ship_status != Order::SHIP_STATUS_RECEIVED) {
                        return $fail('你的订单未收货');
                    }
                }
            ],
            "order_items.*.review" => [
                'required',
                "between:1,150"
            ]
        ];
    }
}
